﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pcb.Common
{
    /// <summary>
    /// 类型转换
    /// </summary>
    public class _Convert
    {
        /// <summary>
        /// 字符串转换为64位长整型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static long ToInt64(string Value)
        {
            return ToInt64(Value, 0);
        }

        /// <summary>
        /// 字符串转换为64位长整型
        /// </summary>
        /// <param name="Value"> 需要转换的字符串 </param>
        /// <param name="DefaultValue"> 默认值 </param>
        /// <returns> 64位长整型值 </returns>
        public static long ToInt64(string Value, long DefaultValue)
        {
            long Result = DefaultValue;

            try
            {
                Result = Convert.ToInt64(Value);
            }
            catch { }

            return Result;
        }

        /// <summary>
        /// 字符串转换为32位长整型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static int ToInt(string Value)
        {
            return ToInt(Value, 0);
        }

        /// <summary>
        /// 字符串转换为32位长整型
        /// </summary>
        /// <param name="Value"> 需要转换的字符串 </param>
        /// <param name="DefaultValue"> 默认值 </param>
        /// <returns> 32位长整型值 </returns>
        public static int ToInt(string Value, int DefaultValue)
        {
            int Result = DefaultValue;

            try
            {
                Result = Convert.ToInt32(Value);
            }
            catch { }

            return Result;
        }

        /// <summary>
        /// 字符串转换为float型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static float ToFloat(string Value)
        {
            return ToFloat(Value, 0);
        }

        /// <summary>
        /// 字符串转换为float型
        /// </summary>
        /// <param name="Value"> 需要转换的字符串 </param>
        /// <param name="DefaultValue"> 默认值 </param>
        /// <returns> 32位长整型值 </returns>
        public static float ToFloat(string Value, float DefaultValue)
        {
            float Result = DefaultValue;

            try
            {
                Result = Convert.ToSingle(Value);
            }
            catch { }

            return Result;
        }

        /// <summary>
        /// 字符串转换为double型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static double ToDouble(string Value)
        {
            return ToDouble(Value, 0);
        }

        /// <summary>
        /// 字符串转换为double型
        /// </summary>
        /// <param name="Value"> 需要转换的字符串 </param>
        /// <param name="DefaultValue"> 默认值 </param>
        /// <returns> 32位长整型值 </returns>
        public static double ToDouble(string Value, double DefaultValue)
        {
            double Result = DefaultValue;

            try
            {
                Result = Convert.ToDouble(Value);
            }
            catch { }

            return Result;
        }

        /// <summary>
        /// 字符串转换为时间类型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(string Value)
        {
            return ToDateTime(Value, DateTime.Now);
        }

        /// <summary>
        /// 字符串转换为时间类型
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(string Value, DateTime DefaultValue)
        {
            DateTime now = DateTime.Now;
            if (DateTime.TryParse(Value, out now))
            {
                return now;
            }
            return DefaultValue;
        }

        /// <summary>
        /// 字符串转换为 decimal 类型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(string Value)
        {
            return ToDecimal(Value, 0M);
        }

        /// <summary>
        /// 字符串转换为 decimal 类型
        /// </summary>
        /// <param name="Value"></param>
        /// <param name="defvalue"></param>
        /// <returns></returns>
        public static decimal ToDecimal(string Value, decimal defvalue)
        {
            decimal result = 0M;
            if (decimal.TryParse(Value, out result))
            {
                return result;
            }
            return defvalue;
        }

        /// <summary>
        /// 字符串转换为 Boolean 型
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static bool ToBool(string Value)
        {
            return ToBool(Value, false);
        }

        /// <summary>
        /// 字符串转换为 Boolean 型
        /// </summary>
        /// <param name="Value"> 需要转换的字符串 </param>
        /// <param name="DefaultValue"> 默认值 </param>
        /// <returns> true false </returns>
        public static bool ToBool(string Value, bool DefaultValue)
        {
            bool Result = DefaultValue;

            try
            {
                Result = Convert.ToBoolean(Value);
            }
            catch { }

            return Result;
        }

    }
}
